Skip to main content

3.1.1 Compressed recoding - inlist

When coding/recoding variables based on long if-conditions, the inlist function can be useful, e.g. if you want to create a variable that takes the value 1 for people who live in a sample of 100 municipalities. This would have given a generate or replace command with a very long if-expression. When using inlist, this can be made more compressed by listing all the municipality numbers separated by commas inside function brackets.

The logical function inlist is set to 1 ("true") if the value in the first argument is found among the remaining arguments.

The arguments to the function can be both variables and values ​​(all types).

Examples:

  • generate var = 1 if inlist(marital_status, 1, 3, 5) (inlist-function = 1 ("true") if marital_status = 1, 3 or 5)

  • generate var = 1 if inlist('1', regstat09, regstat10, regstat11) (inlist function = 1 ("true") if at least one of the regstat-variables = '1')

The examples above correspond to these expressions:

  • generate var = 1 if marital_status == 1 | marital_status == 3 | marital_status == 5

  • generate var = 1 if regstat09 == '1' | regstat10 == '1' | regstat11 == '1'